//index.js
//获取应用实例
import {baseUrl} from '../../api/index'
import {setUserInfo} from '../../utils/setUserInfo.js'
import {shareApp} from '../../utils/shareApp.js'
import {getArticleList, getBanner, getNotice, getStatData} from './../../api/index'
import {syncDianZan} from '../../utils/sync-dianzan.js'
import {getRect} from '../../utils/getRect'
import {scrollFixed} from '../../utils/scroll-fixed'
import {authUserVerify} from '../../utils/authUserVerify'
import {vModel} from '../../utils/v-model'
// import {getUserLocation} from '../../utils/getLocation'

const app = getApp()

// const IMG_LIST = [
//   'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
//   'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
//   'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
// ]
Page({
  data: {
    role: '',
    showSelectRole: false,
    userInfo: {},
    baseUrl: baseUrl + '',
    activeType: 0,
    isLoading: false,
    currentPage: 1,
    hasNextPage: true,
    articleList: [],
    noticeList: [],
    statData: {},
    imgUrls: [],
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  // 导航到搜索页
  bindNavToSearch () {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  bindNavToDetail (e) {
    let _id = e.currentTarget.dataset.id
    if (_id == null || _id === '') return
    authUserVerify('bbs', () => {
      wx.navigateTo({
        url: '/pages/article/index?id=' + _id,
      })
    })
  },
  // 接收子组件点赞状态
  bindItemDianZan (e) {
    syncDianZan(this, e, 'articleList')
  },
  // tab 切换事件
  bindTabChange (e) {
    let _this = this
    _this.setData({
      currentPage: 1,
      hasNextPage: true,
      activeType: e.detail.index
    })
    if (e.detail.index === 0) {
      _this.getArticleList({
        params: {type: 'time', page: 1},
        before () {
          _this.setData({
            articleList: []
          })
        }
      })
    } else {
      _this.getArticleList({
        params: {order: 'comment', page: 1},
        before () {
          _this.setData({
            articleList: []
          })
        }
      })
    }
  },
  //事件处理函数
  bindViewTap () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  // tabbar event
  onTabbarChange (e) {
    console.log(e.detail)
  },
  getCurrentListType () {
    return ['time', 'comment'][this.data.activeType]
  },
  onLoad () {
    console.log(7, 'index')
    setUserInfo(this)
    setTimeout(() => {
      this.setData({
        showSelectRole: true
      })
    }, 2000)
    this.getDataFirstTime('time')
    if (this.data.userInfo.group_id == 2) {
      setTimeout(() => {
        this.setData({
          showSelectRole: true
        })
      }, 2000)
    }
  },
  getDataFirstTime (type, before, cb) {
    setUserInfo(this)
    // 通知
    getNotice().then(res => {
      this.setData({
        noticeList: res.data.info
      })
    })
    // 统计数据
    getStatData().then(res => {
      this.setData({
        statData: res.data.info
      })
    })
    // banner
    getBanner().then(res => {
      this.setData({
        imgUrls: res.data.info
      })
    })
    
    // 首次获取帖子列表
    this.getArticleList({
      params: {type: type, page: 1},
      before () {
        before && before()
      },
      complete () {
        cb && cb()
      }
    })
  },
  /**
   * cfg {object}
   * params {object}参数
   * before {function} 请求正常回调
   * after {function} 请求正常回调
   * complete {function} 请求结束回调
   * */
  getArticleList (cfg = {}) {
    if (this.isLoading) return
    let _this = this
    let _page = _this.data.currentPage
    let _params = Object.assign({}, {page: _page}, (cfg.params || {}), _this.getCurrentListType() === 'time' ? {type: 'time'} : {})
    wx.showLoading({title: '加载中...'})
    getArticleList(_params).then(res => {
      cfg.before && cfg.before()
      let _resInfo = res.data.info
      let _list = _page === 1 ? _resInfo : _this.data.articleList.concat(res.data.info)
      this.setData({
        articleList: _list,
        currentPage: _page + 1,
        hasNextPage: true,
        isLoading: false
      })
      wx.hideLoading()
      cfg.after && cfg.after()
      cfg.complete && cfg.complete()
    }).catch(err => {
      this.setData({
        hasNextPage: false
      })
      wx.hideLoading()
      this.setData({
        isLoading: false
      })
      cfg.complete && cfg.complete()
    })
  },
  onPageScroll () {
    scrollFixed(this, 'fixedTab')
  },
  onShareAppMessage () {
    return shareApp()
  },
  onReachBottom () {
    if (!this.data.hasNextPage) return
    this.getArticleList()
  },
  onPullDownRefresh () {
    let _this = this
    _this.getDataFirstTime(_this.getCurrentListType(), () => {
      wx.stopPullDownRefresh()
      _this.setData({
        articleList: []
      })
    })
  }
})
